package com.hc.action;

import com.hc.domain.Order;
import com.hc.mapper.OrderMapper;
import io.seata.core.context.RootContext;
import io.seata.rm.tcc.api.BusinessActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderActionImpl implements OrderAction {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public void tryOrder(BusinessActionContext context, Order order) {
        System.out.println(context);
        System.out.println(RootContext.getXID());
        System.out.println(RootContext.getBranchType());
        System.out.println(RootContext.inGlobalTransaction());
        orderMapper.addOrder(order);
    }

    @Override
    public void commit(BusinessActionContext context) {
        Order order = context.getActionContext("order", Order.class);
        order.setStatus("success");
        orderMapper.updateOrderStatus(order);
    }

    @Override
    public void rollback(BusinessActionContext context) {
        System.out.println("rollback执⾏了");
        Order order = context.getActionContext("order", Order.class);
        System.out.println(order);
        orderMapper.deleteOrder(order);
    }
}
